<template>
  <!-- v-if控制隐藏，避免价格等信息显示undefined -->
  <view v-if="goods_info.goods_name">
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(pic,i) in goods_info.pics" :key="i">
        <image :src="pic.pics_big" @click="preview(i)"></image>
      </swiper-item>
    </swiper>
    <!-- 商品信息 -->
    <view class="goods-info-box">
      <view class="goods-price">
        ￥{{goods_info.goods_price}}
      </view>
      <view class="goods-info-body">
        <view class="goods-name">
          {{goods_info.goods_name}}
        </view>
        <view class="favi">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <view class="yf">
        运费：免运费
      </view>
    </view>

    <!-- 商品图文详情 -->
    <rich-text :nodes="goods_info.goods_introduce"></rich-text>

    <!-- 商品购物车 -->
    <view class="goods-carts">
      <uni-goods-nav :options="options" :fill="true" :button-group="buttonGroup" @click="onClick"
        @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex' //引入

  export default {
    computed: {
      ...mapState('m_cart', ['cart']),
      //把m_cart模块中名称为total的getter映射到当前的页面中
      ...mapGetters('m_cart', ['total'])
    },
    watch: {
      //函数方式定义的侦听器，在页面首次加载时，并不会立即被调用
      //1.定义监听器，监听total值变化，通过第一个形参，得到变化后的新值
      // total(newVal) {
      // 	// 2.找到购物车按钮的配置对象
      // 	const findResult = this.options.find(x => x.text === '购物车')
      // 	if (findResult) {
      // 		//3.动态为购物车按钮的info属性赋值
      // 		findResult.info = newVal
      // 	}
      // }

      //对象方式定义的侦听器，可配置页面加载时立即调用
      total: {
        handler(newVal) {
          // 1.找到购物车按钮的配置对象
          const findResult = this.options.find(x => x.text === '购物车')
          if (findResult) {
            //2.动态为购物车按钮的info属性赋值
            findResult.info = newVal
          }
        },
        immediate: true,
      }
    },
    data() {
      return {
        goods_info: {},
        options: [{
            icon: 'chat',
            text: '客服'
          },
          {
            icon: 'cart',
            text: '购物车',
            info: 0
          }
        ],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: 'linear-gradient(90deg, #FFCD1E, #FF8A18)',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: 'linear-gradient(90deg, #FE6035, #EF1224)',
            color: '#fff'
          }
        ],
      };
    },
    onLoad(options) {
      console.log(options)
      const goods_id = options.goods_id
      this.getGoodsDetail(goods_id)
    },
    methods: {
      ...mapMutations('m_cart', ['addToCart']),

      async getGoodsDetail(goods_id) {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id: goods_id
        })

        if (res.meta.status !== 200) return uni.$showMsg()

        //使用字符串的replace方法，为img标签添加行内style样式，从而解决图片底部空白间隙的问题
        res.message.goods_introduce = res.message.goods_introduce
          .replace(/<img /g, '<img style="display:block;" ') //解决图片底部空白间隙的问题
          .replace(/webp/g, 'jpg') //处理webp格式图片ios无法显示问题

        this.goods_info = res.message


      },
      preview(i) {
        //调用uni.previewImage()方法预览图片,
        //
        uni.previewImage({
          current: i,
          urls: this.goods_info.pics.map(x => x.pics_big)
        })
      },
      onClick(e) {

        if (e.content.text === '购物车') {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }

      },
      buttonClick(e) {

        if (e.content.text === '加入购物车') {
          const goods = {
            goods_id: this.goods_info.goods_id, //商品的id
            goods_name: this.goods_info.goods_name, //商品的名称
            goods_price: this.goods_info.goods_price, //商品的价格
            goods_count: 1, //商品的数量			
            goods_small_logo: this.goods_info.goods_small_logo, //商品的图片
            goods_state: true //商品的勾选状态
          }

          this.addToCart(goods);
        }
      },

    }
  }
</script>

<style lang="scss">
  swiper {
    height: 750rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }

  .goods-info-box {
    padding: 10px;
    padding-right: 0;

    .goods-price {
      color: #C00000;
      font-size: 18px;
      margin: 10px 0;
    }

    .goods-info-body {
      display: flex;
      justify-content: space-between;

      .goods-name {
        font-size: 13px;
        margin-right: 10px;
      }

      .favi {
        font-size: 12px;
        color: gray;
        width: 120px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        border-left: 1px solid #efefef;
      }

    }

    .yf {
      font-size: 12px;
      color: gray;
      margin: 10px 0;
    }
  }

  .goods-carts {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
  }
</style>